﻿using System;

namespace SDT.CrawlSystem.Servers.Data
{
    /// <summary>
    /// Written by SinhNT
    /// </summary>
    internal static class Repository
    {
        #region Channel
        public static int InsertChannel(string name)
        {
            var dc = new DbDataContext();
            try
            {
                return dc.sp_ad_channels_insert(name, 1000, true);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dc.Dispose();
            }
        }

        public static bool UpdateChannel(int id, string name)
        {
            var dc = new DbDataContext();
            try
            {
                return dc.sp_ad_channels_update((short)id, name, 1000, true) > 0;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dc.Dispose();
            }
        } 
        #endregion

        #region Page
        public static int InsertPage(int cid, string name)
        {
            var dc = new DbDataContext();
            try
            {
                return dc.sp_ad_pages_insert((short)cid, name);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dc.Dispose();
            }
        }

        public static bool UpdateIp(int pid, string ip)
        {
            var dc = new DbDataContext();
            try
            {
                return dc.sp_ad_pages_update_ip(pid, ip) > 0;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dc.Dispose();
            }
        }

        public static bool UpdateName(int pid, string name)
        {
            var dc = new DbDataContext();
            try
            {
                return dc.sp_ad_pages_update_name(pid, name) > 0;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dc.Dispose();
            }
        } 
        #endregion

        #region Url
        public static int InsertUrl(int wid, int pid, int cid, string url)
        {
            var dc = new DbDataContext();
            try
            {
                return dc.sp_ad_urls_insert(wid, pid, (short)cid, url);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                dc.Dispose();
            }
        } 
        #endregion
    }
}